package com.example.demo07cache.jedis;

import java.util.*;

public class LRUCacheDemo {


    static final class Node<K, V> {

        K key;
        V value;

        Node<K, V> prev;
        Node<K, V> next;

        Node() {
        }

        Node(K key, V value) {
            this.key = key;
            this.value = value;
        }
    }

    static class DoubleLinkedList<K, V> {
        Node<K, V> head;
        Node<K, V> tail;

        DoubleLinkedList() {
            head = new Node<>();
            tail = new Node<>();
            head.next = tail;
            tail.prev = head;
        }

        /**
         * 将节点添加到队头
         *
         * @param node
         */
        public void addHead(Node<K, V> node) {
            node.next = head.next;
            node.prev = head;
            head.next.prev = node;
            head.next = node;
        }

        /**
         * 移除节点
         *
         * @param node
         */
        public void removeNode(Node<K, V> node) {
            node.next.prev = node.prev;
            node.prev.next = node.next;
            node.next = null;
            node.prev = null;
        }

        public Node getLast() {
            Node node = tail.prev;
            if (node != null && node.key != null) {
                return node;
            }
            return null;
        }

        public List<K> getList() {
            List<K> list = new ArrayList<>();
            Node<K, V> node = tail.prev;
            while (node != null && node.key != null) {
                list.add(node.key);
                node = node.prev;
            }
            return list;
        }
    }

    private int cacheSize;
    Map<Integer, Node<Integer, Integer>> map;
    DoubleLinkedList<Integer, Integer> doubleLinkedList;

    public LRUCacheDemo(int cacheSize) {
        this.cacheSize = cacheSize;
        map = new HashMap<>();
        doubleLinkedList = new DoubleLinkedList<>();
    }

    public int get(int key) {
        if (!map.containsKey(key)) {
            return -1;
        }

        Node<Integer, Integer> node = map.get(key);
        doubleLinkedList.removeNode(node);
        doubleLinkedList.addHead(node);
        return node.value;
    }

    public void put(int key, int value) {
        if (map.containsKey(key)) { // update
            Node<Integer, Integer> node = map.get(key);
            node.value = value;
            doubleLinkedList.removeNode(node);
            doubleLinkedList.addHead(node);
        } else {
            if (map.size() == cacheSize) { // 坑位满了
                Node<Integer, Integer> lastNode = doubleLinkedList.getLast();
                map.remove(lastNode.key);
                doubleLinkedList.removeNode(lastNode);
            }
            // 新增
            Node<Integer, Integer> newNode = new Node<>(key, value);
            map.put(key, newNode);
            doubleLinkedList.addHead(newNode);
        }

    }

    public static void main(String[] args) {
        LRUCacheDemo cache = new LRUCacheDemo(3);
        cache.put(1, 1);
        cache.put(2, 2);
        cache.put(3, 3);
        System.out.println(cache.doubleLinkedList.getList().toString());

        cache.put(4, 4);
        System.out.println(cache.doubleLinkedList.getList().toString());

        cache.put(3, 3);
        System.out.println(cache.doubleLinkedList.getList().toString());
        cache.put(2, 3);
        System.out.println(cache.doubleLinkedList.getList().toString());
        cache.put(4, 3);
        System.out.println(cache.doubleLinkedList.getList().toString());

//        cache.get(3);
//        System.out.println(cache.doubleLinkedList.getList().toString());
//        cache.get(2);
//        System.out.println(cache.doubleLinkedList.getList().toString());
//        cache.get(4);
//        System.out.println(cache.doubleLinkedList.getList().toString());
    }
}
